#!/bin/sh

SRCDIR="@top_srcdir@"
TEST_SRCDIR="${SRCDIR}/c++/swig/go/tests"
BUILDDIR="@top_builddir@"
TEST_BUILDDIR="${BUILDDIR}/c++/swig/go/tests"

PRINT_MODE=false
while echo $1 | grep '^-' >/dev/null; do
    if [ "X$1" = "X--print" ]; then
	PRINT_MODE=true
    elif [ "X$1" = "X--" ]; then
	break
    else
	echo "Unknown option: $1" 1>&2
	exit 1
    fi
    shift
done

if [ -z "$1" ]; then
    echo "No test given" 1>&2
    exit 1
fi

# See if we are skipping this test
T1=`basename $1`
if echo "$SKIP_TESTS" | grep -q "$T1"; then
    exit 77
fi

TEST=$1
shift

if [ -e "${TEST_BUILDDIR}/${TEST}" ]; then
    TEST="${TEST_BUILDDIR}/${TEST}"
elif [ -e "${TEST_SRCDIR}/${TEST}" ]; then
    TEST="${TEST_SRCDIR}/${TEST}"
else
    echo "Test ${TEST} not found" 1>&2
    exit 1
fi

if [ ! -x ${TEST} ]; then
    echo "Test ${TEST} is not executable" 1>&2
    exit 1
fi

# We need to put the DLL in PATH for MSYS on Windows
if [ ! -z "$MSYSTEM" ]; then
    export PATH=${BUILDDIR}/lib:${BUILDDIR}/lib/.libs:${BUILDDIR}/c++/lib/.libs:${BUILDDIR}/glib/.libs:${BUILDDIR}/glib/c++/swig/pygensio/.libs:${BUILDDIR}/tcl/.libs:${BUILDDIR}/tcl/c++/swig/pygensio/.libs:$PATH
else
    export LD_LIBRARY_PATH=${BUILDDIR}/lib:${BUILDDIR}/lib/.libs:${BUILDDIR}/c++/swig/python/.libs:${BUILDDIR}/glib/.libs:${BUILDDIR}/glib/c++/pygensio/.libs:${BUILDDIR}/tcl/.libs:${BUILDDIR}/tcl/c++/pygensio/.libs:${BUILDDIR}/c++/lib/.libs
fi
if test `uname` = Darwin; then
    TEST="DYLD_LIBRARY_PATH=${LD_LIBRARY_PATH} ${TEST}"
fi

if ${PRINT_MODE}; then
    echo export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}"
    echo ${TEST} $*
else
    # Run the test with each available OS handler
    eval "${TEST} $*"
    rv=$?
    if test $rv != 0; then
        # A test failed, exit
	exit $rv
    fi
fi
